﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace tsp_states
{
    class Program
    {
        
        static void Main(string[] args)
        {
            List<Node> NodeList = new List<Node>();
            List<string> names=new List<string>();
            string filename = @"..\..\graph.txt";
            string [] nodestrings = System.IO.File.ReadAllLines(filename);

            // this is probably inefficient but the code is simple so who cares
            // first create every node
            foreach (var node in nodestrings)
            {
                if (String.IsNullOrEmpty(node))
                    continue;
                string[] connections = node.Split(' ');
                Node n = new Node(connections[0]);
                NodeList.Add(new Node(connections[0]));
            }

            // then connect all the nodes
            foreach (var node in nodestrings)
            {
                string[] connections = node.Split(' ');
                Node n = NodeList.Find(item => item.Name.Equals(connections[0]));
                for (int i = 1; i < connections.Length; i++)
                {
                    if (String.IsNullOrEmpty(connections[i]))
                        continue;
                    Node c = NodeList.Find(item => item.Name.Equals(connections[i]));
                    n.AddEdge(c);
                }
            }

            //foreach (var node in NodeList)
            //{
            //    Console.WriteLine(node.Name);
            //    var edges = node.GetEdges();
            //    foreach (var edge in edges)
            //    {
            //        Console.WriteLine("  -> {0}", edge);
            //    }
            //}

            Node deleware = NodeList.Find(item => item.Name.Equals("deleware"));
            List<Node> route = new List<Node>();
            bool found = FindRoute(deleware, route);

            if (found)
            {
                Console.WriteLine("Route from Deleware:");
                foreach( var node in route)
                {
                    Console.WriteLine(node.Name);
                }
            }
        }

        static bool FindRoute(Node node, List<Node> route)
        {
            if (node.Visited == true)
                return false;

            route.Add(node);
            node.Visited = true;
            if (route.Count == 48)
                return true;

            var connections = node.GetEdges();
            bool found = false;
            foreach (var connection in connections)
            {
                var next = node.TravelTo(connection);
                if (next.Visited == true)
                    continue;

                found = FindRoute(next, route);
                if (found)
                    break;
            }

            if (!found)
            {
                route.Remove(node);
                node.Visited = false;
            }
            return found;
        }
    }
}
